home *** CD-ROM | disk | FTP | other *** search
/ Plug-In Power Pack for Netscape Communicator / Plug-In Power Pack for Netscape Communicator.iso / plugins / dataviews / dvtools / examples / programs / symload.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-08  |  15.5 KB  |  461 lines

  1. #ifndef lint
  2. static char SccsId[]= "@(#)symload.c    V1.27    3/13/95";
  3. #endif
  4. /*
  5. |    file name - symload.c
  6. |===================================================================
  7. |
  8. |    This program manages views created in DV-Draw. Objects are named
  9. |    according to the filename of the view that should be displayed.
  10. |    When a named object is selected the code finds the appropriate
  11. |    view/drawport and draws it to the front.
  12. |
  13. |    This program uses DV-Tools' symbol table routines to set up the
  14. |    table of drawports and view names.
  15. |
  16. |    The programs ends if the user selects the <q|Q> key or the
  17. |    right mouse button.  It also ends if the user selects an
  18. |    object named "quit".
  19. |
  20. |===================================================================
  21. */
  22. #include <windows.h>
  23. /*
  24.  *  DV-Tools header files
  25.  */
  26. #include "std.h"                /* <stdio.h> etc., scalar & macro definitions */
  27. #include "dvstd.h"              /* public types & constants */
  28. #include "dvtools.h"            /* constants used by T routines */
  29. #include "dvGR.h"               /* constants used by window mgt & GR routines */
  30. #include "VOstd.h"              /* constants used by VO & VOob routines */
  31. #include "Tfundecl.h"           /* T routines (screens, drawports & views) */
  32. #include "VOfundecl.h"          /* VO routines (objects) */
  33. #include "VTfundecl.h"          /* VT routines (hash & symbol table) */
  34. #include "VUerfundecl.h"        /* VUer routines (event handling routines) */
  35. #include "VUfundecl.h"          /* VU routines (utilities) */
  36.  
  37. /* Constants */
  38. #define  DVPATH            (char *)NULL
  39. #define  DISPFORMS_STB     (char *)NULL
  40. #define  DVDEVICE          (char *)NULL
  41. #define  DVCOLORTABLE      (char *)NULL
  42. #define  VIEW_NAME         "dvtooldemo.v"
  43. #define  SCREEN_VIEWPORT   (RECTANGLE *)NULL
  44. #define  DRAWING_VIEWPORT  (RECTANGLE *)NULL
  45.  
  46. /* Define global variables */
  47. OBJECT         screen;          /* display device, the window */
  48. DATASOURCELIST master_dsl;      /* master data source list */
  49. SYMTABLE       NameTable;       /* symbol table */
  50.  
  51. /* Functions defined in symload.c */
  52. DRAWPORT SetupDrawports V_P_((char *top_view_name));
  53. ADDRESS AddDrawports V_P_((OBJECT object, char *view_name, ADDRESS argblock));
  54. DRAWPORT GetNewDrawport V_P_((char *obj_name));
  55. void FreeDrawports V_P_((void));
  56.  
  57.  
  58. /*
  59.  *   MAIN PROGRAM
  60.  */
  61. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  62.                      LPSTR lpCmdLine,  int nCmdShow  )
  63. {
  64.   INT argc = 0;
  65.   CHAR **argv;
  66.  
  67.   /*
  68.    *  program arguments
  69.    *    argv[1] - display device (default is DVDEVICE)
  70.    *    argv[2] - view filename
  71.    */
  72.  
  73.   /* Define & initialize device name */
  74.   char *device_name = DVDEVICE; /* default device name */
  75.  
  76.   /* Control loop variables */
  77.   OBJECT location;              /* the event representation */
  78.   int event_req_status;         /* status of event requests */
  79.  
  80.   /* Other variables */
  81.   DRAWPORT current_drawport,    /* how & where to display picture */
  82.     next_drawport;              /* how & where to display picture */
  83.   char *obj_name = VIEW_NAME;   /* name of view file to display */
  84.   int Quit = NO;                /* flag to quit program */
  85.  
  86.   /*-----------------
  87.    *   Initialization
  88.    *
  89.    *   TInit:    perform the initialization of DV-Tools
  90.    *             TInit reads your configuration file and any
  91.    *             environment variables or logical names set.
  92.    */
  93.   make_argv(&argc,&argv,GetCommandLine());
  94.   TInit( DVPATH, DISPFORMS_STB );
  95.  
  96.   /*
  97.    *   TscOpenSet:  opens a device as a screen object using
  98.    *                specified attributes
  99.    *   TscErase:    Erases the display device
  100.    *
  101.    *   Set exposure block to YES to insure the window
  102.    *   is ready for drawing when TdpDraw is called.
  103.    */
  104.   if (argc > 1)
  105.     device_name = argv[1];
  106.   screen = TscOpenSet (device_name, DVCOLORTABLE,
  107.                        V_X_EXPOSURE_BLOCK, YES,
  108.                        V_ACTIVE_CURSOR, V_END_OF_LIST);
  109.   if (!screen)
  110.     {
  111.       printf ("Must specify device on command line or");
  112.       printf (" in DataViews configuration file.\n");
  113.       S_EXIT (EXIT_ERR);
  114.     }
  115.   TscErase (screen);
  116.  
  117.   /*
  118.    *   VOscWinEventMask:  sets the screen's window event mask
  119.    */
  120.   VOscWinEventMask ((ULONG) V_KEYPRESS | V_KEYRELEASE |
  121.                             V_BUTTONPRESS | V_BUTTONRELEASE |
  122.                             V_RESIZE | V_EXPOSE | V_MOTIONNOTIFY,
  123.             (ULONG) 0);
  124.  
  125.   /*
  126.    *   If the user supplies an initial view, create a drawport
  127.    *   with it; otherwise, use the default view name,
  128.    *   "dvtooldemo.v".  Then draw the initial drawport.
  129.    *   If the current drawport has been assigned a NULL value
  130.    *   then there was a problem loading in the initial view.
  131.    *   Therefore, we exit the program.
  132.    */
  133.   if (argc > 2)
  134.     obj_name = argv[2];
  135.   current_drawport = SetupDrawports (obj_name);
  136.   if (!current_drawport)
  137.     {
  138.       printf ("Could not load view from file ");
  139.       printf ("%s.\n", obj_name);
  140.       S_EXIT (EXIT_ERR);
  141.     }
  142.  
  143.   /*
  144.    *  TdlOpenData: Reads all data for one iteration
  145.    *  TdlReadData: Opens all files and processes
  146.    *  TdpDraw:       Draw the contents of the drawport
  147.    */
  148.   TdlOpenData (master_dsl);
  149.   TdlReadData (master_dsl);
  150.   TdpDraw (current_drawport);
  151.  
  152.   FOREVER
  153.   {
  154.     /*
  155.      * VOloWinEventPoll:   Poll for the next window event.
  156.      *                     The polling mode used is V_NO_WAIT.
  157.      *                     Using this mode, VOloWinEventPoll
  158.      *                     does not wait until a masked event
  159.      *                     is generated.
  160.      *
  161.      * VUerHandleLocEvent: Service the event. This routine will check
  162.      *                     if the event is used by any input objects.
  163.      *                     that are in the view.
  164.      */
  165.     location = VOloWinEventPoll (V_NO_WAIT);
  166.     if (location)
  167.       {
  168.         event_req_status = VUerHandleLocEvent (location);
  169.         if (event_req_status == INPUT_UNUSED)
  170.           {
  171.             /*
  172.              *  VOloType:  returns the type of event.  These types
  173.              *             match event types specified in VOscWinEventMask.
  174.              */
  175.             switch (VOloType (location))
  176.               {
  177.  
  178.               case V_RESIZE:
  179.                 /*
  180.                  *  The window size has been changed.
  181.                  *  TscReset:  Resets all screen drawports after
  182.                  *             window resizing
  183.                  */
  184.                 TscReset (screen);
  185.                 break;
  186.  
  187.               case V_EXPOSE:
  188.                 /*
  189.                  *  VOloRegion:  Returns a rectangle representing the
  190.                  *               exposed region on the screen.
  191.                  *  TscRedraw:   After erasing, redraws all the drawports
  192.                  *               in the screen.
  193.                  *  A portion of the window has been exposed and needs
  194.                  *  to be redrawn.
  195.                  */
  196.                 TscRedraw (screen, VOloRegion (location));
  197.                 break;
  198.  
  199.               case V_KEYPRESS:
  200.                 /*
  201.                  *  Check key selected.
  202.                  *  VOloKeySym:  Returns the key symbol value of the
  203.                  *               location object
  204.                  *
  205.                  *  If the key symbol represents the characters 'q'
  206.                  *  or 'Q' then quit the program.
  207.                  */
  208.                 switch (VOloKeySym (location))
  209.                   {
  210.                   case 'q':
  211.                   case 'Q':
  212.                     Quit = YES;
  213.                     break;
  214.  
  215.                   default:
  216.                     break;
  217.                   }
  218.                 break;
  219.  
  220.               case V_BUTTONPRESS:
  221.                 /*
  222.                  *  Check button selected.
  223.                  *  VOloButton:  Returns the button that was pressed
  224.                  *  TloGetSelectedObjectName: Get the name of the selected 
  225.                  *  object.
  226.                  *  The left mouse button acts as the selection button. If
  227.                  *  the object selected by the user has a name then compare
  228.                  *  this name to the exit string "quit". If they match then
  229.                  *  exit the program. Otherwise, use the object name as the
  230.                  *  name of the next view file to display.
  231.                  */
  232.                 switch (VOloButton (location))
  233.                   {
  234.                   case 1:
  235.                     obj_name = TloGetSelectedObjectName (location);
  236.                     if (obj_name)
  237.                       {
  238.                         if (strcmp (obj_name, "quit") == 0)
  239.                           Quit = YES;
  240.                         else {
  241.                               next_drawport = GetNewDrawport (obj_name); 
  242.                               if (next_drawport)
  243.                               {
  244.                                 TdpErase (current_drawport);
  245.                                 current_drawport = next_drawport;
  246.                                 TdpDraw (current_drawport);
  247.                                }
  248.                               else{
  249.                                    printf ("Could not load view from file ");
  250.                                    printf ("%s.\n", obj_name);
  251.                               }  
  252.                           }
  253.                       }
  254.                     break;
  255.  
  256.                   case 3:
  257.                     Quit = YES;
  258.                     break;
  259.  
  260.                   default:
  261.                     break;
  262.                   }
  263.                 break;
  264.  
  265.               case V_MOTIONNOTIFY:
  266.               default:
  267.                 break;
  268.               }
  269.           }
  270.       }
  271.  
  272.     if (Quit == YES)
  273.       break;
  274.     TdlReadData (master_dsl);
  275.     TdpDrawNext (current_drawport);
  276.   }
  277.  
  278.   /*--------------------
  279.    *   Termination
  280.    *
  281.    *   TscCloseCurrentScreen:  Close the current display screen
  282.    *   TTerminate:   Perform the clean-up for DV-Tools
  283.    */
  284.   TdlCloseData (master_dsl);
  285.   FreeDrawports ();
  286.   TscCloseCurrentScreen ();
  287.   TTerminate ();
  288.   return EXIT_OK;
  289. }
  290.  
  291.  
  292. /*-----------------
  293.  *   SetupDrawports --  create the main drawport to display the
  294.  *    specified view. Use it's data source list for the master
  295.  *    data source. Create the symbol table which will hold the
  296.  *    created drawports. Then traverse all named objects in the
  297.  *    main view and create drawports for each named object which
  298.  *    corresponds to a view file.
  299.  */
  300. DRAWPORT 
  301. SetupDrawports (top_view_name)
  302.      char *top_view_name;
  303. {
  304.   DRAWPORT top_drawport;
  305.   VIEW top_view;
  306.   OBJECT drawing;
  307.  
  308.   /*
  309.    *   TviLoad:   Load a view in from a file, the main view
  310.    *              is a user specified view filename or the
  311.    *              default view filename, dvtooldemo.v
  312.    *   TviGetDataSourceList:  Get the data source list.
  313.    *
  314.    *   Create a master data source list from the using the top view
  315.    */
  316.   top_view = TviLoad (top_view_name);
  317.   if (!top_view)
  318.     return NULL;
  319.   master_dsl = TviGetDataSourceList (top_view);
  320.  
  321.   /*
  322.    *   TdpCreate: Create a drawport.
  323.    *              The drawport is attached to the screen object
  324.    *              specified while view specifies the view to be
  325.    *              displayed on the screen.
  326.    */
  327.   top_drawport = TdpCreate (screen, top_view,
  328.                             SCREEN_VIEWPORT, DRAWING_VIEWPORT);
  329.  
  330.   /*
  331.    *   VTstcreate:    Create a symbol table
  332.    *   VTstsinsert:    Inserts a node in the symbol table
  333.    *   TviGetDrawing:   Gets drawing object
  334.    *   TdrForEachNamedObject:  Traverse all named objects in drawing
  335.    *
  336.    *   Create a symbol table of drawports which are keyed by their
  337.    *   view's name.  Traverse all named objects in the top level view.
  338.    *   Attempt to load in a view using the object's name. If this is
  339.    *   successful create a drawport for this view and store the drawport
  340.    *   in the symbol table.
  341.    */
  342.   NameTable = VTstcreate ("Object Names and Drawports", (VTSTCOMPAREFUNPTR)NULL);
  343.   VTstsninsert (NameTable, VUstrClone (top_view_name), (int *) top_drawport);
  344.   drawing = TviGetDrawing (top_view);
  345.   TdrForEachNamedObject (drawing, (TDRFOREACHNAMEDOBJFUNPTR)AddDrawports, (ADDRESS) NULL);
  346.   return top_drawport;
  347. }
  348.  
  349.  
  350. /*---------------
  351.  *   AddDrawports --  loads the named view and creates a
  352.  *     drawport for it.  Stores the new drawport in the symbol table.
  353.  */
  354. /* ARGSUSED */
  355. ADDRESS 
  356. AddDrawports (object, view_name, argblock)
  357.      OBJECT object;
  358.      char *view_name;
  359.      ADDRESS argblock;
  360. {
  361.   DRAWPORT drawport;
  362.   VIEW view;
  363.  
  364.   /*
  365.    *   VTstkeyfind:  Returns address of specified key in symbol table
  366.    *   TviMergeAddDataSource:  Merges/add data sources.
  367.    *
  368.    *   If the name of the object is "quit", there is no view to be
  369.    *   loaded, continue with the traversal.  Otherwise, attempt to
  370.    *   load the view, and if successful, check to see if the view is
  371.    *   already in the symbol table.  If not, add the views'
  372.    *   datasources to the master datasource list, create a drawport,
  373.    *   and add the drawport to the symbol table.
  374.    */
  375.   if (strcmp (view_name, "quit") == 0)
  376.     return V_CONTINUE_TRAVERSAL;
  377.   if (!VTstkeyfind (NameTable, view_name)){
  378.     view = TviLoad (view_name);
  379.     if (view)
  380.       {
  381.         TviMergeAddDataSources (view, master_dsl, DS_NAMEMATCH);
  382.         drawport = TdpCreate (screen, view, SCREEN_VIEWPORT, DRAWING_VIEWPORT);
  383.         VTstsninsert (NameTable, VUstrClone (view_name), (int *) drawport);
  384.       }
  385.     }
  386.  
  387.   return V_CONTINUE_TRAVERSAL;
  388. }
  389.  
  390.  
  391. /*-----------------
  392.  *   GetNewDrawport --  uses an object name to search the symbol table
  393.  *     for the next drawport to be displayed. If the view file name
  394.  *     does not exist in the symbol table then attempt to load in this
  395.  *     view file name, create a drawport for it and add the drawport to
  396.  *     the symbol table. Finally, return the drawport.
  397.  */
  398. DRAWPORT 
  399. GetNewDrawport (obj_name)
  400.      char *obj_name;
  401. {
  402.   SYMNODE symbol_node;
  403.   DRAWPORT drawport;
  404.   VIEW view;
  405.  
  406.   symbol_node = VTstkeyfind (NameTable, obj_name);
  407.   if (!symbol_node)
  408.     {
  409.       view = TviLoad (obj_name);
  410.       if (view)
  411.         {
  412.           TviMergeAddDataSources (view, master_dsl, DS_NAMEMATCH);
  413.           drawport = TdpCreate (screen, view, SCREEN_VIEWPORT, 
  414.                                 DRAWING_VIEWPORT);
  415.           VTstsninsert (NameTable, VUstrClone (obj_name), (int *) drawport);
  416.           return drawport;
  417.         }
  418.       else
  419.         return NULL;
  420.     }
  421.   else
  422.     return (DRAWPORT) VTsnvalue (symbol_node);
  423. }
  424.  
  425.  
  426. /*----------------
  427.  *   FreeDrawports --  Destroy drawports and associated views
  428.  *    freeing allocated memory. Remove the nodes from the
  429.  *    symbol table and destroy the symbol table.
  430.  */
  431. void FreeDrawports ()
  432. {
  433.   DRAWPORT drawport;
  434.   VIEW view;
  435.  
  436.   /*
  437.    *   VTstlen:        Returns number of nodes in symbol table
  438.    *   VTstsnremove:    Remove node from symbol table
  439.    *   TdpGetView:    Gets view of a drawport
  440.    *   TdpDestroy:    Destroy the drawport
  441.    *   TviDestroy:    Destroy the view, freeing the allocated memory
  442.    *
  443.    *   Traverse the symbol table. Obtain each drawport and
  444.    *   destroy it and it's view. Then remove the node from the
  445.    *   symbol table.
  446.    */
  447.   while (VTstlen (NameTable) > 0)
  448.     {
  449.       drawport = (DRAWPORT) VTsnvalue (VTstsnget (NameTable, 0));
  450.       view = TdpGetView (drawport);
  451.       TdpDestroy (drawport);
  452.       TviDestroy (view);
  453.       VTstsnremove (NameTable, VTstsnget (NameTable, 0));
  454.     }
  455.  
  456.   /*
  457.    *   VTstdestroy:  Destroy the symbol table.
  458.    */
  459.   VTstdestroy (NameTable);
  460. }
  461.